package com.chao.deeply.future;

import java.util.concurrent.*;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/5/8
 */
public class FutureTaskTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        ExecutorService executorService = Executors.newCachedThreadPool();

        FutureTask<String> futureTask1 = new FutureTask<>(() -> {
            TimeUnit.SECONDS.sleep(3);
            return "调用了call方法！";
        });

        String res = null;

        FutureTask<String> futureTask2 = new FutureTask<>(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("输出run方法被调用！");
        }, res);

        executorService.execute(futureTask1);
        executorService.execute(futureTask2);

        System.out.println(futureTask1.get(2,TimeUnit.SECONDS));
        System.out.println(futureTask2.get(2,TimeUnit.SECONDS));
    }
}
